package dao.impl;

import dao.ClinicsDao;
import entity.Clinics;
import handler.StringListHandler;
import org.apache.commons.dbutils.QueryRunner;
import utils.DruidUtils;

import java.sql.Connection;
import java.util.List;

public class ClinicsDaoImpl implements ClinicsDao {
    @Override
    public Clinics queryById(Integer id) {
        return null;
    }

    @Override
    public long count(Clinics clinics) {
        return 0;
    }

    @Override
    public int insert(Clinics clinics) {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "insert into clinics ( de_class, department) values (?,?)";
            QueryRunner queryRunner = new QueryRunner();
            int update = queryRunner.update(conn, s, clinics.getDeClass(), clinics.getDepartment());
            DruidUtils.close(null, conn);
            if (update > 0) return update;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return 0;
    }

    @Override
    public int update(Clinics clinics) {
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }

    @Override
    public List<String> findAllClass() {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "SELECT DISTINCT de_class FROM clinics;";
            QueryRunner queryRunner = new QueryRunner();
            List<String> query = queryRunner.query(conn, s, new StringListHandler());
            DruidUtils.close(null, conn);
            return query;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return null;
    }

    @Override
    public List<String> findAllDepartment(String depart) {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "SELECT  department FROM clinics where de_class=? ";
            QueryRunner queryRunner = new QueryRunner();
            List<String> query = queryRunner.query(conn, s,
                    new StringListHandler(), depart);
            DruidUtils.close(null, conn);
            return query;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return null;
    }
}
